package org.example.aienglishapp.service.login;

import org.example.aienglishapp.dto.login.PermissionDTO;
import org.example.aienglishapp.dto.login.RoleDTO;
import org.example.aienglishapp.entity.login.Permission;
import org.example.aienglishapp.entity.login.Role;
import org.example.aienglishapp.mapper.login.RoleMapper;
import org.example.aienglishapp.mapper.login.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色服务类。
 *
 * 该类提供了角色管理相关的业务逻辑，包括获取角色信息、为权限分配和移除角色等操作。
 *
 * @author xlj
 * @since 2024-10-1
 * @version 1.0
 */
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 获取所有角色及其关联的权限信息。
     *
     * @return 返回包含所有角色及其权限信息的列表
     */
    @Transactional(readOnly = true)
    public List<RoleDTO> getAllRolesWithPermissions() {
        List<Role> roles = roleMapper.findAllRoles();

        List<RoleDTO> roleDTOs = roles.stream()
                .map(role -> {
                    RoleDTO roleDTO = new RoleDTO();
                    roleDTO.setRoleId(role.getRoleId());
                    roleDTO.setRoleName(role.getRoleName());

                    Set<PermissionDTO> permissionDTOS = role.getPermissions().stream()
                            .map(permission -> {
                                PermissionDTO permissionDTO = new PermissionDTO();
                                permissionDTO.setPermissionId(permission.getPermissionId());
                                permissionDTO.setPermissionName(permission.getPermissionName());
                                return permissionDTO;
                            })
                            .collect(Collectors.toSet());

                    roleDTO.setPermissions(permissionDTOS);

                    // 输出调试信息
                    System.out.println("Role: " + roleDTO.getRoleName());
                    for (PermissionDTO permissionDTO : permissionDTOS) {
                        System.out.println("  - Permission: " + permissionDTO.getPermissionName());
                    }

                    return roleDTO;
                })
                .collect(Collectors.toList());

        return roleDTOs;
    }

    /**
     * 为指定权限添加角色。
     *
     * @param permissionId 权限ID
     * @param roleId       角色ID
     * @return 如果操作成功返回 true，否则返回 false
     */
    @Transactional
    public boolean addRoleToPermission(Long permissionId, Long roleId) {
        Permission permission = permissionMapper.findById(permissionId);
        Role role = roleMapper.findById(roleId);

        if (permission != null && role != null) {
            permission.getRoles().add(role);
            permissionMapper.updatePermission(permission);
            permissionMapper.insertPermissionRole(permissionId, roleId);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 从指定权限中移除角色。
     *
     * @param permissionId 权限ID
     * @param roleId       角色ID
     * @return 如果操作成功返回 true，否则返回 false
     */
    @Transactional
    public boolean removeRoleFromPermission(Long permissionId, Long roleId) {
        Permission permission = permissionMapper.findById(permissionId);
        Role role = roleMapper.findById(roleId);

        if (permission != null && role != null) {
            permission.getRoles().remove(role);
            permissionMapper.updatePermission(permission);
            permissionMapper.deletePermissionRole(permissionId, roleId);
            return true;
        } else {
            return false;
        }
    }
}